<?php


namespace app\api\controller;


use app\api\model\LogisticsOutlets as BZModel;
use think\facade\Request;

class LogisticsOutlets extends Common
{
    /**
     * @param BZModel $model
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(BZModel $model, Request $request)
    {
        if ($request::has('pid')) {
            $logistics = $request::param('pid', '');
        } else {
            $logistics = $model->logistic()->fetchCollection()->select()->column('id');
        }
        $res = $model->whereIN('logistics_id', $logistics)
            ->getModel()->index($request::param('page', 1), $request::param('size', 10));
        return is_string($res) ? $this->returnJson(500, $res) : $this->returnJson(200, 'ok', $res);
    }

    /**
     * @param BZModel $model
     * @param Request $request
     * @return \think\response\Json
     */
    public function store(BZModel $model, Request $request)
    {
        $data = $request::param();
        $res = $model->store($data);
        return is_string($res) ? $this->returnJson(500, $res) : $this->returnJson(200, 'ok', $res);
    }

    /**
     * @param $id
     * @param BZModel $model
     * @return \think\response\Json
     */
    public function show($id, BZModel $model)
    {
        $res = $model->show($id);
        return is_string($res) ? $this->returnJson(500, $res) : $this->returnJson(200, 'ok', $res);
    }

    /**
     * @param $id
     * @param BZModel $model
     * @param Request $request
     * @return \think\response\Json
     */
    public function update($id, BZModel $model, Request $request)
    {
        $data = $request::param();
        $res = $model->edit($id, $data);
        return is_string($res) ? $this->returnJson(500, $res) : $this->returnJson(200, 'ok', $res);
    }

    /**
     * @param $id
     * @param BZModel $model
     * @return \think\response\Json
     */
    public function delete($id, BZModel $model)
    {
        $res = $model->del($id);
        return is_string($res) ? $this->returnJson(500, $res) : $this->returnJson(200, 'ok', $res);
    }
}
